package com.huabo.cybermonitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.JsonBean;
import com.huabo.cybermonitor.entity.*;
import com.huabo.cybermonitor.mapper.YhrPageMapper;
import com.huabo.cybermonitor.service.*;
import com.huabo.cybermonitor.service.base.CJobTaskService;
import com.huabo.cybermonitor.service.impl.AutoIdService;
import com.huabo.cybermonitor.task.base.JobTaskService;
import com.huabo.cybermonitor.task.base.ScheduleJob;
import com.huabo.cybermonitor.util.ConstClass;
import com.huabo.cybermonitor.util.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.*;

import static org.apache.http.util.TextUtils.isEmpty;

/**
 * 规则监控-规则预警api接口
 *
 * @createTime 2022/8/12
 */

@RestController
@Slf4j
@Api(value = "规则监控-规则预警", tags = {"规则监控-规则预警下所有接口"})
@RequestMapping(value = "/cyber/GzyjController")
@SuppressWarnings("all")
public class GzyjController {

    @Autowired
    IOrganizationService iOrganizationService;

    @Autowired
    IMonitorSolutionService iMonitorSolutionService;

    @Autowired
    IMonitorRuleService iMonitorRuleService;

    @Autowired
    IMonitorSolutionRuleService iMonitorSolutionRuleService;

    @Autowired
    IOrganizationService iOrganizaService;

    @Autowired
    CJobTaskService cJobTaskService;

    @Autowired
    JobTaskService jobTaskService;

    @Autowired
    IStaffService iStaffService;

    @Autowired
    IMonitorPrewarningService iMonitorPrewarningService;


    @Autowired
    IMonitorSolutionresultService iMonitorSolutionresultService;

    /**
     * 规则预警---左侧树形
     */
    @GetMapping(value = "/gzjk/rule/solutionleft")
    @ApiOperation("规则预警---左侧树形")
    public JsonBean solutionleft(@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        Map<String, Object> mv = new HashMap<>();
        // 在token 取到当前用户
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        Organization byId = iOrganizationService.getById(staff.getStaffid());
        mv.put("userOrgId", byId.getOrgid());
        mv.put("orgid", staff.getCurrentOrg().getOrgid());

        return new JsonBean(200, "成功", mv);
    }


    @Autowired
    YhrPageMapper yhrPageMapper;
    /**
     * 规则预警 --列表
     */
    @GetMapping(value = "/gzjk/rule/solutionmgmt")
    @ApiOperation("规则预警 --列表")
    public JsonBean solutionMgmt(@ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId",required = false) BigDecimal orgId,
                                 @ApiParam(name = "solutioncode", value = "solutioncode") @RequestParam(value = "solutioncode") String solutioncode,
                                 @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber", name = "pageNumber", defaultValue = "1") Integer pageNumber,
                                 @ApiParam(name = "pageSize", value = "pageSize") @RequestParam(value = "pageSize", name = "pageSize", defaultValue = "15") Integer pageSize,
                                 @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        // 在token 取到当前用户
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        IPage<Map<String,Object>> a = new Page<>(pageNumber, pageSize);
        StringBuilder where  = new StringBuilder(" where ");
        Boolean isSelect = false;
        boolean isDU = false;
        if (staff.getCurrentOrg().getOrgid().equals(staff.getLinkOrg().getOrgid())) {
            if (orgId == null) {
                orgId = staff.getLinkDetp().getOrgid();
            }

            where.append(" a.type =  1");
            where.append(" and a.ORGID = "+orgId);
            if (StringUtils.isNotBlank(solutioncode)) {
                where.append(" and a.SOLUTIONCODE like "+solutioncode);
            }
            where.append(" order by CREATEDATE");

        } else {
            if (orgId == null) {
                orgId = staff.getCurrentOrg().getOrgid();
            }
            where.append(" a.type =  1");
            where.append(" and a.ORGID = "+orgId);
        }
        Map<String, Object> mv = new HashMap<>();
        mv.put("orgId", orgId);

        String sql = "select a.*,ts.realname from TBL_MONITOR_SOLUTION a left join TBL_STAFF TS on a.STAFFID = TS.STAFFID" +where;
        yhrPageMapper.getPage(a,sql);
        mv.put("pageBean", a);
        mv.put("isAdd", isSelect);
        mv.put("isDU", isDU);
        mv.put("solutioncode", solutioncode);
        return new JsonBean(200, "成功", mv);

    }

    /**
     * 规则预警--添加跳转
     */
    @GetMapping(value = "/gzjk/rule/to_solution_add")
    @ApiOperation("规则预警--添加跳转")
    public JsonBean toSolutionAdd(@ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId") BigDecimal orgId,
                                  @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        Map<String, Object> mv = new HashMap<>();
        mv.put("orgId", orgId);
        mv.put("tblStaff", staff);
        return new JsonBean(200, "成功", mv);
    }

    /**
     * 规则预警--保存
     */
    @GetMapping(value = "/gzjk/rule/solution_add")
    @ApiOperation("规则预警--保存")
    @Transactional
    public JsonBean solutionAdd(@ApiParam(name = "solutioncode", value = "solutioncode") @RequestParam(value = "solutioncode") String solutioncode,
                                @ApiParam(name = "solutionname", value = "solutionname") @RequestParam(value = "solutionname") String solutionname,
                                @ApiParam(name = "solutionstatus", value = "solutionstatus") @RequestParam(value = "solutionstatus") String solutionstatus,
                                @ApiParam(name = "memo", value = "memo") @RequestParam(value = "memo") String memo,
                                @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token
    ) throws Exception {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        // 在token 取到当前用户
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        MonitorSolution solution = new MonitorSolution();
        solution.setSolutioncode(solutioncode);
        solution.setSolutionname(solutionname);
        solution.setRunstatus(new BigDecimal(0));
        solution.setSolutionstatus(solutionstatus);
        solution.setType(new BigDecimal(1));
        solution.setCreatedate(LocalDateTime.now());
        solution.setMemo(memo);
        Organization organization = this.iOrganizaService.getById(staff.getLinkDetp().getOrgid());
        if (null != organization) {
            solution.setOrgid(organization.getOrgid());
        }
        solution.setStaffid(staff.getStaffid());
        //solution.setSolutionid(new BigDecimal("1559526045026635778"));
        iMonitorSolutionService.save(solution);
        return new JsonBean(200, "成功", 0);
    }


    /**
     * 规则预警--添加规则列表
     */
    @GetMapping(value = "/gzjk/rule/ruleslistSelector")
    @ApiOperation("规则预警--添加规则列表")
    public JsonBean ruleslistSelector(@ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId") BigDecimal orgId,
                                      @ApiParam(name = "pageNumber", value = "pageNumber") @RequestParam(value = "pageNumber") Integer pageNumber,
                                      @ApiParam(name = "pageSize", value = "pageSize") @RequestParam("pageSize") Integer pageSize,
                                      @ApiParam(name = "solutionid", value = "solutionid") @RequestParam(value = "solutionid",required = false) String solutionid,
                                      @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        IPage<MonitorRule> ip = new Page<>(pageNumber, pageSize);
        QueryWrapper<MonitorRule> wrapper = new QueryWrapper<>();
        wrapper.eq("orgid", orgId);
        wrapper.eq("satus", "启用");
        Map<String, Object> mv = new HashMap<>();
        mv.put("pageBean", iMonitorRuleService.page(ip, wrapper));
        mv.put("orgId", orgId);
        mv.put("solutionid", solutionid);
        return new JsonBean(200, "成功", mv);
    }


    /**
     * 规则预警-保存规则
     */
    @GetMapping(value = "/gzjk/add_rule")
    @ApiOperation("规则预警-保存规则")
    public JsonBean add_rule(@ApiParam(name = "ruleid", value = "ruleid") @RequestParam(value = "ruleid") String ruleid,
                             @ApiParam(name = "souceid", value = "souceid") @RequestParam(value = "souceid") String souceid,
                             @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        if (ruleid != null && souceid != null) {


            QueryWrapper qw = new QueryWrapper();
            qw.eq("SOLUTIONID", souceid);
            qw.eq("RULEID", ruleid);
            MonitorSolutionRule msr = iMonitorSolutionRuleService.getOne(qw);
            if (msr == null) {
                msr = new MonitorSolutionRule();
                msr.setRuleid(new BigDecimal(ruleid));
                msr.setSolutionid(new BigDecimal(souceid));
                iMonitorSolutionRuleService.save(msr);
            }
//            MonitorSolution solution = iMonitorSolutionService.getById(souceid);
//            MonitorRule r = iMonitorRuleService.getById(ruleid);
//            MonitorSolution monitorSolution = iMonitorSolutionService.getById(souceid);
//            MonitorSolutionRule sr = new MonitorSolutionRule();
//            sr.setRuleid(r.getRuleid());
//            sr.setSolruleid(solution.getSolutionid());
//            QueryWrapper qw = new QueryWrapper();
//            qw.eq("solutionid", solution.getSolutionid());
//            List<MonitorSolutionRule> list = iMonitorSolutionRuleService.list(qw);
//            boolean fal = true;
//            if (list != null && list.size() > 0) {
//                for (MonitorSolutionRule iMonitorSolutionRule : list) {
//                    if (iMonitorSolutionRule.getRuleid().toString().equals(ruleid)
//                            && iMonitorSolutionRule.getSolutionid().toString()
//                            .equals(souceid)) {
//                        fal = false;
//                    }
//                }
//            }
//            if (fal) {
//                iMonitorSolutionRuleService.save(sr);
//                iMonitorSolutionService.updateById(monitorSolution);
//            }
        }
        return new JsonBean(200, "成功", "");
    }

    /**
     * 规则预警--修改跳转
     */
    @GetMapping(value = "/gzjk/rule/to_solution_modify")
    @ApiOperation("规则预警--修改跳转")
    public JsonBean toSolutionModify(@ApiParam(name = "selectedid", value = "selectedid") @RequestParam(value = "selectedid") String selectedid,
                                     @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        MonitorSolution solution = iMonitorSolutionService.getById(selectedid);

        List<MonitorRule> list = iMonitorSolutionRuleService.queryRuleBySoluid(new BigDecimal(selectedid));

        StringBuffer buffer = new StringBuffer();
        if (list != null) {
            for (MonitorRule mr : list) {
                buffer.append(mr.getRuleid() + ",");
            }
        }

        Map<String, Object> mv = new HashMap<>();
        mv.put("solutionruleList", list);
        if (buffer.toString().length() > 0) {
            mv.put("solruleids", buffer.toString().substring(0, buffer.toString().length() - 1));
        }
        if (solution != null) {
            mv.put("solution", solution);
            mv.put("orgId", solution.getOrgid());
        }
        return new JsonBean(200, "成功", mv);
    }

    /**
     * 规则预警--判断当前方案是否可以删除或者修改
     *   if ("solution_del".equals(href)) {
     *                     return new JsonBean(200, "方案已执行，不能删除！", "方案已执行，不能删除！");
     *                 } else {
     *                     return new JsonBean(200, "方案已执行，不能修改！", "方案已执行，不能修改！");
     *                 }
     */
    @GetMapping(value = "/gzjk/rule/solutioncheckUpdate")
    @ApiOperation("规则预警 修改|删除的前置判断--判断当前方案是否可以删除或者修改[solution_del 为删除前判断，其它为修改前判断]")
    public JsonBean solutionchechUpate(@ApiParam(name = "selectedid", value = "方案编号") @RequestParam(value = "selectedid") String selectedid,
                                       @ApiParam(name = "href", value = "修改以及删除时状态判断，solution_del为删除，其它为修改") @RequestParam(value = "href") String href,
                                       @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        // 在token 取到当前用户
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        MonitorSolution rule = this.iMonitorSolutionService.getById(selectedid);
        if(rule==null) return new JsonBean(200, "表中没有此条数据", selectedid);
        if (rule.getStaffid().equals(staff.getStaffid())) {
            if (!rule.getRunstatus().equals(0)) {
                if ("solution_del".equals(href)) {
                    return new JsonBean(200, "方案已执行，不能删除！", "方案已执行，不能删除！");
                } else {
                    return new JsonBean(200, "方案已执行，不能修改！", "方案已执行，不能修改！");
                }
            }
            return new JsonBean(1, "可以进行修改", "");
        } else {
            return new JsonBean(200, "权限不足", "权限不足");
        }
    }

    /**
     * 规则预警 -修改操作
     */
    @GetMapping(value = "/gzjk/rule/solution_modify")
    @ApiOperation("规则预警--修改操作")
    public JsonBean solutionModify(@ApiParam(name = "solutionid", value = "solutionid") @RequestParam(value = "solutionid") String solutionid,
                                   @ApiParam(name = "solutioncode", value = "solutioncode") @RequestParam(value = "solutioncode") String solutioncode,
                                   @ApiParam(name = "solutionname", value = "solutionname") @RequestParam(value = "solutionname") String solutionname,
                                   @ApiParam(name = "solutionstatus", value = "solutionstatus") @RequestParam(value = "solutionstatus") String solutionstatus,
                                   @ApiParam(name = "memo", value = "memo") @RequestParam(value = "memo") String memo,
                                   @ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId") String orgId,
                                   @ApiParam(name = "createdate", value = "2000-10-12") @RequestParam(value = "createdate") String createdate,
                                   @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token)
            throws ParseException {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        MonitorSolution solution = this.iMonitorSolutionService.getById(solutionid);
        solution.setSolutioncode(solutioncode);
        solution.setSolutionname(solutionname);
        solution.setSolutionstatus(solutionstatus);
        //Organization organization = this.iOrganizaService.getById(orgId);
        //solution.setOrgid(organization.getOrgid());
        solution.setOrgid(new BigDecimal(orgId));
        if (createdate != null && !"".equalsIgnoreCase(createdate)) {
            solution.setCreatedate(DateUtils.StringToLocalDateTime(createdate, "yyyy-MM-dd"));
        }
        solution.setMemo(memo);
        Map<String, Object> mv = new HashMap<>();
        iMonitorSolutionService.updateById(solution);
        return new JsonBean(200, "成功", mv);
    }

    /**
     * 规则预警 -删除操作
     */
    @GetMapping(value = "/gzjk/rule/solution_del")
    @ApiOperation("规则预警--删除操作")
    public JsonBean solutionDel(@ApiParam(name = "selectedid", value = "selectedid") @RequestParam(value = "selectedid") String selectedid,
                                @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        Map<String, Object> mv = new HashMap<>();
        if (selectedid != null) {
            MonitorSolution monitorSolution = this.iMonitorSolutionService.getById(selectedid);
            mv.put("orgId", monitorSolution.getOrgid());
            // iMonitorSolutionService.removeById(selectedid);  删除操作
        }
        return new JsonBean(200, "成功", mv);
    }

    /**
     * 预警方案--状态修改
     */
    @GetMapping(value = "/gzjk/solution/solutionUpdateStatus")
    @ApiOperation("预警方案--状态修改")
    public JsonBean solutionUpdateStatus(@ApiParam(name = "solutionid", value = "136494") @RequestParam(value = "solutionid") String solutionid,
                                         @ApiParam(name = "pageNumber", value = "1") @RequestParam(value = "pageNumber") String pageNumber,
                                         @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {
        if(!ConstClass.checkToken(token)){
            return ConstClass.tokenFailure();
        }
        // 在token 取到当前用户
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        MonitorSolution solution = this.iMonitorSolutionService.getById(solutionid);
        if (solution.getStaffid().equals(staff.getStaffid())) {
            if (null != solution) {
                String status = solution.getSolutionstatus();
                if (status.equalsIgnoreCase("启用")) {
                    solution.setSolutionstatus("停用");
                } else if (status.equalsIgnoreCase("停用")) {
                    solution.setSolutionstatus("启用");
                }
                iMonitorSolutionService.updateById(solution);
                return new JsonBean(200, "成功", solution);
            }
        }
        return new JsonBean(200, "权限不足", "权限不足");
    }

    /**
     * 查看规则预警执行结果
     */
    @GetMapping(value = "/result/resultmgmtyj")
    @ApiOperation("查看规则预警执行结果")
    public JsonBean resultmgmtyj(@ApiParam(name = "pageNumber", value = "1") @RequestParam(value = "pageNumber", name = "pageNumber", defaultValue = "1") Integer pageNumber,
                                 @ApiParam(name = "pageSize", value = "20") @RequestParam(value = "pageSize", name = "pageSize", defaultValue = "15") Integer pageSize,
                                 @ApiParam(name = "source", value = "source",defaultValue = "") @RequestParam(value = "source", required = false) String source,
                                 @ApiParam(name = "soultionId", value = "soultionId",defaultValue = "199979") @RequestParam(value = "soultionId") String soultionId,
                                 @ApiParam(name = "orgId", value = "orgId",defaultValue = "127634") @RequestParam(value = "orgId") String orgId,
                                 @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }

        IPage<MonitorSolutionresultVo> ip = new Page<>(pageNumber, pageSize);
        String sql="select tms.*,ts.REALNAME,ts.USERNAME from TBL_MONITOR_SOLUTIONRESULT tms inner join TBL_STAFF TS on tms.STAFFID = TS.STAFFID\n" +
                "where SOLUTIONID="+soultionId+" ";

        if (!isEmpty(soultionId)) {
            if (StringUtils.isNotBlank(source)) {
                if (source.equals("3")) {
                    sql+=" and source="+MonitorSolutionresult.ZKZX;

                } else {
                    sql+=" and rownum=1 and  source="+MonitorSolutionresult.YJYJ;

                }
            } else {
                sql+=" and  source="+MonitorSolutionresult.ZX;

            }
            sql+= " order by saveTime desc ";
        }
        this.iMonitorSolutionresultService.getSolutionresultVoPage(ip,sql);
        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("orgId", orgId);
        mv.put("soultionId", soultionId);
        mv.put("source", source);
        mv.put("pageBean",ip);


        return new JsonBean(200, "成功", mv);
    }

    /**
     * 规则预警 --结果页面
     */
    @GetMapping(value = "/result/resultmgmtlist")
    @ApiOperation("规则预警 --结果页面")
    public JsonBean resultmgmtlist(@ApiParam(name = "modelId", value = "modelId") @RequestParam(value = "modelId") BigDecimal modelId,
                                   @ApiParam(name = "solutionResultId", value = "solutionResultId") @RequestParam(value = "solutionResultId") BigDecimal solutionResultId,
                                   @ApiParam(name = "soultionId", value = "soultionId") @RequestParam(value = "soultionId") String soultionId,
                                   @ApiParam(name = "source", value = "source") @RequestParam(value = "source", required = false) String source,
                                   @ApiParam(name = "orgId", value = "orgId") @RequestParam(value = "orgId",required = true) String orgId,
                                   @ApiParam(name = "fhtype", value = "fhtype") @RequestParam(value = "fhtype", required = false) String fhtype,
                                   @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        List<MonitorPrewarning> resultBySignId = iMonitorPrewarningService.getResultBySignId(modelId, solutionResultId);
        Map<String, Object> mv = new HashMap<>();
        mv.put("solutionResultId", solutionResultId);
        mv.put("soultionId", soultionId);
        mv.put("modelId", modelId);
        mv.put("fhtype", fhtype);
        mv.put("orgId", orgId);
        mv.put("results", resultBySignId);
        mv.put("source", source);
        return new JsonBean(200, "成功", mv);
    }

    /**
     * 规则预警 --执行
     */
    @GetMapping(value = "/gzjk/rule/solutionExecute")
    @ApiOperation("规则预警 --执行")
    public JsonBean solutionExecute(@ApiParam(name = "selectedid", value = "selectedid") @RequestParam(value = "selectedid") String selectedid,
                                    @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        // 在token 取到当前用户
        TblStaffUtil staff = DealUserToken.parseUserToken(token);//staff
        MonitorSolution solution = iMonitorSolutionService.getById(selectedid);
        if (null != solution && solution.getSolutionstatus().equals("启用")) {
            if (solution.getRunstatus().equals("1")) {
                return new JsonBean(200, "正在执行,请稍等！", "正在执行,请稍等！");
            } else {
                List<MonitorSolution> solutions = new ArrayList<MonitorSolution>();
                solutions.add(solution);
                ScheduleJob job = null;
                try {
                    Staff staff1 = iStaffService.getById(staff.getStaffid());
                    //执行类不匹配
                    job = this.cJobTaskService.gzzjTaskExec(solutions, staff1, "", MonitorSolutionresult.ZX);
                } catch (SchedulerException e) {
                    e.printStackTrace();
                    try {
                        this.jobTaskService.changeStatus(job, "stop");
                    } catch (SchedulerException e1) {
                        e1.printStackTrace();
                    }
                }
                return new JsonBean(200, "成功", "");
            }
        }
        return new JsonBean(200, "请先启用规则", "请先启用规则");
    }

    @Autowired
    AutoIdService autoid;
    /**
     * 通用获取设置的编号
     *
     * @param
     */
    @GetMapping(value = "/code/findAutoNumber")
    @ApiOperation("规则监控-规则预警-编号生成(这是通用方法,通过改变传值生成各类编号)")
    public JsonBean findAutoNumber(HttpServletResponse response,
                                   @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
                                   @ApiParam(name = "tblName", value = "TBL_ASSESS", defaultValue = "TBL_ASSESS") @RequestParam(value = "tblName") String tblName,
                                   @ApiParam(name = "column", value = "ASSESSID", defaultValue = "ASSESSID") @RequestParam(value = "column") String column,
                                   @ApiParam(name = "orgCol", value = "TBLCOMANY", defaultValue = "TBLCOMANY") @RequestParam(value = "orgCol") String orgCol,
                                   @ApiParam(name = "noId", value = "noId", defaultValue = "309") @RequestParam(value = "noId") Integer noId) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }
        TblStaffUtil staff = DealUserToken.parseUserToken(token);
        String flowNextId = null;
        try {
            flowNextId = autoid.findFlowNextId(tblName, column, orgCol, staff.getCurrentOrg().getOrgid(), noId,
                    null, null, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.reset();
        return new JsonBean(200, "success", flowNextId);
    }

    /**
     * 获取组织是否使用自定义编号
     * @param orgid
     * @return
     * @throws Exception
     */
    public Integer getIsUseAutoNoInfo(BigDecimal orgid) throws Exception {
        String sql = "SELECT ISAUTONUMBER FROM TBL_ORGANIZATION WHERE ORGID = "+orgid;
        Organization o =  iOrganizaService.getById(orgid);
        return Integer.parseInt(o.getIsautonumber().toString());
    }



    /**
     * 规则预警-查看
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/gzjk/rule/solution_disp_gz")
    @ApiOperation("规则预警-查看")

    public JsonBean solution_disp_gz(@ApiParam(name = "selectedid", value = "selectedid") @RequestParam(value = "selectedid") String selectedid,
                                     @ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) throws Exception {
        if (!ConstClass.checkToken(token)) {
            return ConstClass.tokenFailure();
        }


        MonitorSolution solution=iMonitorSolutionService.queryOne(selectedid);

        List<MonitorRule> list= iMonitorSolutionService.queryMonitorRuleList(selectedid);

        Map<String, Object> mv = new LinkedHashMap<>();
        mv.put("solution", solution);
        mv.put("orgId", solution.getOrgid());
        mv.put("solutionruleList", list);



        return new JsonBean(200, "success", mv);
    }

}
